/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.pay.component;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Pair;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.qihoo.gamecenter.sdk.common.i.s;

public final class PaySelectorGrid extends LinearLayout implements View.OnClickListener {
    private int a = 4;
    private int b;
    private int c = 1;
    private int d = -3355444;
    private boolean e = true;
    private View f;
    private View[] g;
    private String[] h;
    private a i;
    private SparseArray j;

    public PaySelectorGrid(Context paramContext) {
        super(paramContext, null);
        this.b = s.b(paramContext, 9.0F);
        setOrientation(1);
    }

    public void setSeparatorLineColor(int paramInt) {
        this.d = paramInt;
        setBackgroundColor(this.d);
    }

    public void setItemPadding(int paramInt) {
        this.b = s.b(getContext(), Math.max(paramInt, 2));
    }

    public void setSeparatorLineSize(int paramInt) {
        this.c = Math.max(1, paramInt);
    }

    public void setSeparatorBorderEnable(boolean paramBoolean) {
        this.e = paramBoolean;
    }

    public PaySelectorGrid a(int paramInt) {
        if (this.g == null)
            throw new IllegalArgumentException(
                    "@SelectorGridLayout: bindData(...) wasn't called, please exeucte it be first!");
        if (paramInt < 0)
            paramInt = 0;
        else if (paramInt >= this.g.length)
            paramInt = this.g.length - 1;
        onClick(this.g[paramInt]);
        return this;
    }

    public void onClick(View paramView) {
        if (this.f != null) {
            localPair = (Pair) this.f.getTag();
            this.i.c(this.f, (String) localPair.first, ((Integer) localPair.second).intValue());
            this.f.setPadding(0, this.b, 0, this.b);
        }
        Pair localPair = (Pair) paramView.getTag();
        this.f = paramView;
        this.i.b(paramView, (String) localPair.first, ((Integer) localPair.second).intValue());
        paramView.setPadding(0, this.b, 0, this.b);
    }

    public void a() {
        if (this.f == null)
            return;
        Pair localPair = (Pair) this.f.getTag();
        this.i.c(this.f, (String) localPair.first, ((Integer) localPair.second).intValue());
        this.f = null;
    }

    public PaySelectorGrid b(int paramInt) {
        if (1 > paramInt)
            paramInt = 1;
        else if (10 < paramInt)
            paramInt = 10;
        this.a = paramInt;
        return this;
    }

    public PaySelectorGrid a(String[] paramArrayOfString) {
        if (this.i == null)
            throw new IllegalArgumentException("@SelectorGridLayout: your must give a selector factory.");
        if ((paramArrayOfString == null) || (paramArrayOfString.length <= 0))
            throw new IllegalArgumentException(
                    "@SelectorGridLayout: selectable item's count must greater than zero(0).");
        this.h = new String[paramArrayOfString.length];
        System.arraycopy(paramArrayOfString, 0, this.h, 0, paramArrayOfString.length);
        paramArrayOfString = null;
        b(this.h);
        return this;
    }

    private void b() {
        if (this.j == null)
            this.j = new SparseArray();
        else
            this.j.clear();
        if ((this.g == null) || (this.g.length <= 0))
            return;
        a();
        for (int k = 0; k < this.g.length; ++k) {
            View localView = this.g[k];
            ViewParent localViewParent = localView.getParent();
            if (localViewParent instanceof ViewGroup)
                ((ViewGroup) localViewParent).removeView(localView);
            this.j.append(k, localView);
        }
    }

    private void b(String[] paramArrayOfString) {
        if (this.e)
            setPadding(this.c, this.c, 0, 0);
        setBackgroundColor(this.d);
        int k = getMeasuredWidth() / this.a;
        LinearLayout localLinearLayout1 = c(1);
        LinearLayout localLinearLayout2 = null;
        int l = paramArrayOfString.length % this.a;
        int i1 = paramArrayOfString.length / this.a;
        if (l != 0)
            ++i1;
        b();
        this.g = new View[paramArrayOfString.length];
        for (int i2 = 0; i2 < i1 * this.a; ++i2) {
            localObject1 = a(1.0F / this.a, 65283, this.c);
            ((LinearLayout.LayoutParams) localObject1).width = k;
            if (!(this.e)) {
                if ((i2 + 1) % this.a == 0)
                    ((LinearLayout.LayoutParams) localObject1).rightMargin = 0;
                if (i1 * this.a - i2 <= this.a)
                    ((LinearLayout.LayoutParams) localObject1).bottomMargin = 0;
            }
            if (i2 % this.a == 0) {
                localLinearLayout2 = c(0);
                localLinearLayout1.addView(localLinearLayout2, a(0.0F, 65282));
            }
            if (i2 < paramArrayOfString.length) {
                View localView = this.i.a((View) this.j.get(i2), paramArrayOfString[i2], i2);
                if (localView == null)
                    throw new IllegalArgumentException(
                            "@SelectorGridLayout: selector view can't be null, ISelectorFactory must return non-null view.");
                localObject2 = localView.getLayoutParams();
                if (localObject2 != null)
                    ((LinearLayout.LayoutParams) localObject1).height = ((ViewGroup.LayoutParams) localObject2).height;
                localView.setPadding(0, this.b, 0, this.b);
                localView.setLayoutParams((ViewGroup.LayoutParams) localObject1);
                localView.setOnClickListener(this);
                localView.setTag(new Pair(paramArrayOfString[i2], Integer.valueOf(i2)));
                this.g[i2] = localView;
                localLinearLayout2.addView(localView);
            }
            else {
                localObject2 = new TextView(getContext());
                ((TextView) localObject2).setText("");
                ((TextView) localObject2).setPadding(0, this.b, 0, this.b);
                if (this.d == 0)
                    ((TextView) localObject2).setBackgroundColor(0);
                else
                    ((TextView) localObject2).setBackgroundColor(Color.parseColor("#efefef"));
                ((TextView) localObject2).setLayoutParams((ViewGroup.LayoutParams) localObject1);
                localLinearLayout2.addView((View) localObject2);
            }
        }
        removeAllViews();
        addView(localLinearLayout1, 0, a(0.0F, 65282));
        Pair localPair = this.i.b();
        if (localPair == null)
            return;
        Object localObject1 = (String) localPair.second;
        Object localObject2 = this.i.c();
        if ((localObject2 == null) || (localPair.first == null) || (TextUtils.isEmpty((CharSequence) localObject1))
                || (TextUtils.isEmpty(((String) localObject1).trim())) || ("null".equals(localObject1))
                || ("NULL".equals(localObject1)))
            return;
        LinearLayout localLinearLayout3 = c(0);
        addView(localLinearLayout3, 1, a(0.0F, 65282));
        LinearLayout.LayoutParams localLayoutParams = a(1.0F / this.a, 65283, this.c);
        localLayoutParams.width = k;
        ((View) localPair.first).setPadding(0, this.b, 0, this.b);
        localLinearLayout3.addView((View) localPair.first, localLayoutParams);
        localLayoutParams = a((this.a - 1) * 1.0F / this.a, 65283, this.c);
        localLayoutParams.width = (k * (this.a - 1) + this.c * (this.a - 1));
        localLinearLayout3.addView((View) localObject2, localLayoutParams);
    }

    private LinearLayout c(int paramInt) {
        LinearLayout localLinearLayout = new LinearLayout(getContext());
        localLinearLayout.setOrientation(paramInt);
        return localLinearLayout;
    }

    private LinearLayout.LayoutParams a(float paramFloat, int paramInt) {
        return a(paramFloat, paramInt, 0);
    }

    private LinearLayout.LayoutParams a(float paramFloat, int paramInt1, int paramInt2) {
        LinearLayout.LayoutParams localLayoutParams = null;
        switch (paramInt1) {
        case 65281:
            localLayoutParams = new LinearLayout.LayoutParams(-1, -1);
            break;
        case 65282:
            localLayoutParams = new LinearLayout.LayoutParams(-1, -2);
            break;
        case 65283:
            localLayoutParams = new LinearLayout.LayoutParams(-2, -1);
            break;
        default:
            localLayoutParams = new LinearLayout.LayoutParams(-2, -2);
        }
        localLayoutParams.weight = Math.max(0.0F, paramFloat);
        localLayoutParams.rightMargin = paramInt2;
        localLayoutParams.bottomMargin = paramInt2;
        return localLayoutParams;
    }

    public PaySelectorGrid a(a parama) {
        if (parama == null)
            throw new IllegalArgumentException("@SelectorGridLayout: your must give a selector factory.");
        this.i = parama;
        return this;
    }

    public static abstract interface a {
        public abstract View a(View paramView, String paramString, int paramInt);

        public abstract boolean b(View paramView, String paramString, int paramInt);

        public abstract boolean c(View paramView, String paramString, int paramInt);

        public abstract Pair b();

        public abstract View c();
    }
}